var eventHandler = function() {
	
	/*
	 * create blog related
	 */
	
	// popup create form
	$("#create").click(function(e) {
		
		// cancel the link behavior
		e.preventDefault();		
		$(this).colorbox({inline:true, width:"50%"});		
	});
	
	// submit create - parse form values to the ajax function createPost
	$("#create_submit").click(function(e) {
		
		// prevent default action
		e.preventDefault();		
		// get value from the form inputs
		var author = $("#create_user").val();
		var password = $("#create_pw").val();
		var title = $("#create_title").val();
		var content = $("#create_content").val();		
		createPost(author, password, title, content);
	});
	
	// cancel create - clear form value
	$("#create_cancel").click(function() {
		$("#create_user").val("");
		$("#create_pw").val("");
		$("#create_title").val("");
		$("#create_content").val("");		
	});
	
	
	/*
	 * comment form related
	 */
	
	// validate comment form
	function validComment(user, content) {
		msg = "";
		if( user == "" ){
			msg += "Please leave your name.\n";
		}
		if( content == "") {
			msg += "Please leave your comment.\n";
		}
		if (msg != "") {
			alert(msg);
			return false;
		}
		return true;		
	}
	
	// submit comment - parse form values to the ajax function createComment
	$("#comment_submit").click(function(e) {
		// cancel the link behavior
		e.preventDefault();
		
		// get value from the form inputs
		var pid = $("#postID").val();
		var user = $("#user").val();
		var content = $("#comment_content").val();
		var rate = $("#rate_form").val();
		
		if (validComment(user, content) == false) {
			return false;
		}

		addComment(pid, user, content, rate);
	});

	
	/*
	 * rating related
	 */
	
	$("#ratePost>li>a").click(function(e) {
		e.preventDefault();
		// get rate value
		var rate = $(this).attr("title");
		// change class to change star style
		$("#ratePost").removeClass().addClass("rating rate" + rate);
		// modify value in hidden field
		$("#rate_form").val(rate);
	});
	
};


/**
 * calculate how many stars to show 
 * then add corresponding class to rate result of post
 * @method showPostrate
 */
function showPostrate() {
	// rate value is stored in hidden field passed by django tags
	var rate = parseInt($("#postrate").val());
	// calculate to the up-bounding value from (2, 4, 6, 8, 10) 
	rate = (Math.ceil(rate/2))*2;
	// add class to postrating 
	$("#postrating").addClass("rate" + rate);
}

/**
 * convert the content using regular expression
 * @method convertContent
 */
function convertContent() {
	content = $("#post_content").html();
	content = content.replace(/\n\n/g, "</p><p>");
	content = content.replace(/\n/g, "<br />");
	$("#post_content").html(content);
}

/**
 * control the scroll to top button
 * source from http://webdesignerwall.com/tutorials/animated-scroll-to-top
 * @method scrollTop
 */
function scrollTop() {
	
	// hide back-top button at first
	$("#back-top").hide();
	
	// fade in #back-top
	$(function () {
		$(window).scroll(function () {
			if ($(this).scrollTop() > 100) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		$('#back-top a').click(function () {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	});
	
}

function setup() {
	eventHandler();
	showPostrate();
	convertContent();
	scrollTop();
}

$(document).ready(setup);